<?php

namespace App\Service\ServiceTrait;

use App\Amqp\Producer\InviteVideoProducer;
use App\Cache\InviteVideoCache;
use App\Constants\InviteVideoCode;
use App\Model\InviteVideoLog;
use App\Model\Message;
use App\Model\UserProfile;
use App\Model\VideoCall;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Amqp\Producer;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

trait InviteVideoCheckTrait
{
    use GetUserInfoTrait;

    /**
     * 给女生发送视频邀约弹窗
     * @param $userId
     * @param $hostProfile
     * @return void
     */
    public function sendInviteTip($userId,$hostProfile)
    {
        //添加数据
        $model = new InviteVideoLog();
        $model->male_id = $hostProfile->user_id;
        $model->female_id = $userId;
        $model->status = InviteVideoLog::STATUS_INVITE;
        $model->save();
        if (!$model->save()){
            log_error('InviteVideoCheckTrait:insertDataError');
        }

        //发送透传消息
        Message::sendSlient($userId,'CALL_INVITE',[
            'title' => '温馨通话福利',
            'sub_title' => '对方是新人用户，邀请他',
            'avatar' => UserProfile::buildAvatar($hostProfile->avatar, 200),
            'invite_title' => '视频通话'.InviteVideoCode::FREE_TIME.'分钟',
            'invite_sub_title' => '可获得'.InviteVideoCode::PLATFORM_SUBSIDY_PRICE.'金币奖励~',
            'close_time' => (InviteVideoCode::AUTO_CANCEL * 60),
            'host_user_id' => $hostProfile->user_id//内部id
        ]);

        //锁定用户
        InviteVideoCache::lockUserInviteVideo($userId,0);
        InviteVideoCache::lockUserInviteVideo($hostProfile->user_id,$hostProfile->gender);
    }

    /**
     * 检测是否繁忙
     * @param $userId
     * @return false|Builder|Model|mixed|object|null
     */
    public function checkUserCallStatus($userId)
    {
        $profile = $this->getUserInfo($userId);

        if (InviteVideoCache::isLock($userId,$profile->gender)){
            return false;
        }

        if ($profile->online == 0){
            return false;
        }

        $permitField = 'video_permit';
        if ($profile->{$permitField} == 0) {
            return false;
        }

        //对方繁忙
        if (
            VideoCall::checkUserChatting($userId)
        ) {
            return false;
        }

        return $profile;
    }

    /**
     * 确认女用户状态
     * @param $userId
     * @return bool
     */
    public function checkFemaleUserCallStatus($userId): bool
    {


        $profile = $this->getUserInfo($userId);

        //需要踢出数据
        if ($profile->online == 0){
            return false;
        }

        $permitField = 'video_permit';
        if ($profile->{$permitField} == 0) {
            return false;
        }

        //对方繁忙
        if (
            VideoCall::checkUserChatting($userId)
        ) {
            return false;
        }

        return true;
    }

    /**
     * 重载数据
     * @param $maleId
     * @param $femaleId
     * @param $status
     * @return void
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function reloadData($maleId,$femaleId,$status){
        //先解锁
        InviteVideoCache::delLock($femaleId,0);
        InviteVideoCache::delLock($maleId,1);

        //更新状态
        InviteVideoLog::updateStatus($maleId,$femaleId,$status);

        //删除两者今天的逻辑关系
        InviteVideoCache::delInviteRefuse($maleId,$femaleId);

        //重新进队列
        $this->runInvite($maleId);
        $this->runInvite($femaleId);
    }

    /**
     * 跑数据
     * @param $userId
     * @return void
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function runInvite($userId){
        $message = new InviteVideoProducer(['user_id' => $userId]);
        $producer = ApplicationContext::getContainer()->get(Producer::class);
        $producer->produce($message);
    }

    /**
     * 确认男用户状态
     * @param $userId
     * @param $payList
     * @return bool
     */
    public function checkMaleStatus($userId,$payList): bool
    {
        if (in_array($userId,$payList)){
            return false;
        }

        //当日拒绝的出去
        if (InviteVideoLog::getInviteVideoRefuseCountToday($userId) >= InviteVideoCode::TODAY_MAX_REFUSE_NUM){
            return false;
        }

        //单日最大接听已经过了出去
        if (InviteVideoLog::getInviteVideoCountToday($userId) >= InviteVideoCode::TODAY_MAX_ANSWER_NUM){
            return false;
        }

        //总数最大接听已经过了出去
        if (InviteVideoLog::getInviteVideoCountTotal($userId) >= InviteVideoCode::TOTAL_MAX_ANSWER_NUM){
            return false;
        }

        return true;
    }
}